<?php
include_once 'Protocol.php';
//创建Server对象，监听 127.0.0.1:9620端口，类型为SWOOLE_SOCK_UDP
$serv = new swoole_server("0.0.0.0", 9620, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);

//监听数据接收事件
$serv->on('Packet', function ($serv, $data, $clientInfo) {
    $data = bin2hex($data);
    echo('data: ' . $data . "\n");
    $device_imei = substr($data, 1, 16);

    $smoke = new Protocol($data, $clientInfo);
    if ($smoke->checkData() == false) {
        echo("check data failed \n");
        return false;
    }
    echo('data info: ' . json_encode($smoke->dataInfo(), JSON_UNESCAPED_UNICODE) . "\n");


    $str = $smoke->sendData();
    echo('服务端返回数据:' . $str . "\n");
    $serv->sendto($clientInfo['address'], $clientInfo['port'], hex2bin($str));
});

//启动服务器
$serv->start();